/**
* @Title: warning_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('WarningScreenListCtrl', ['$scope','$http','FwaTabsetService','$modal','BaseURL','Modal','WarningService',
	                                function($scope,$http,FwaTabsetService,$modal,baseUrl,Modal,WarningService){
		$scope.warningService = WarningService;
		WarningService.initMedicalOrgInfoList();
		$scope.form = {};
		$scope.form.warnDate = {};
		var currentPage = 1;
		var cellTemplate = 
			'<td>'+
				'<a  ng-click="clickLink(\'edit\',data)"">{{data.tranName}}</a>'+
			'</td>',
			cellTemplateView = 
				'<td>'+
				   '<a title=\'{{data.warnNoBiz}}\' ng-click="clickLink(\'warnQueryScreen.warningView\',data)"">{{data.warnNoBiz}}</a>'+
			    '</td>',
			indexNo = '<td>'+
				'{{$index+1}}'+
			'</td>';
       $scope.columns = [
        {label:'预警编号',width:'11%',cellClass:'text-left', cellTemplate:cellTemplateView},
        {label:'预警事件名称',map:'summary',width:'15%'},
        {label:'机构名称',map:'medicalOrgName',width:'18%'},
        {label:'处理方式',map:'process',width:'11%'},
        {label:'预警时段',map:'happenDate',width:'14%'},       
        {label:'受理时间',map:'warnDate',width:'11%'},        
        {label:'事件状态',map:'actName',width:'11%'},
        {label:'操作',width:'9%',cellClass:'text-left', cellTemplate:cellTemplate}

    ];
       $scope.clickLink = function (url,data){
    	    if(url==='edit'){
    		   FwaTabsetService.selectTab('warnQueryScreen.warningScreenEdit',{warnNo:data.warnNo,urlFlag:''},false);
    	   }else{
    		   FwaTabsetService.selectTab(url,{warnNo:data.warnNo,actNo:'screen',isBack:'N'},false);
    	   }
       }

		//每页显示数
        var start = 0,
            limit = 20;
        
//        $scope.form = {
//            page:"0",
//            pageSize:"20"
//        };

        
        var params = function (){
			var warnNoBiz=null;
			if($scope.form.warnNoBiz&&!angular.isUndefined($scope.form.warnNoBiz)){
				warnNoBiz = $scope.form.warnNoBiz;
			}
			var medicalOrgCode=null;
			if($scope.form.medicalOrgCode&&!angular.isUndefined($scope.form.medicalOrgCode)){
				medicalOrgCode = $scope.form.medicalOrgCode;
			}
			var summary=null;
			if($scope.form.summary&&!angular.isUndefined($scope.form.summary)){
				summary = $scope.form.summary;
			}
			var feedbackEndDate=null;
			if($scope.form.feedbackEndDate&&!angular.isUndefined($scope.form.feedbackEndDate)){
				feedbackEndDate = $scope.form.feedbackEndDate;
			}
//			var actNo=null;
//			if($scope.form.actNo&&!angular.isUndefined($scope.form.actNo)){
//				actNo = $scope.form.actNo;
//			}
			var startDate=null;
			if($scope.form.warnDate.start&&!angular.isUndefined($scope.form.warnDate.start)){
				startDate = $scope.form.warnDate.start;
			}
			var endDate=null;
			if($scope.form.warnDate.end&&!angular.isUndefined($scope.form.warnDate.end)){
				endDate = $scope.form.warnDate.end;
			}
			
			if(parseInt(currentPage) > 1){
                  start = (parseInt(currentPage) - 1) * limit;
            }
			var params = {
					warnNoBiz:warnNoBiz,
					medicalOrgCode:medicalOrgCode,
					summary:summary,
					feedbackEndDate:feedbackEndDate,
//					actNo:actNo,
					startDate:startDate,
					endDate:endDate,
					start:start,
					limit:limit
			}
			return params;
		}
        
        //查询
        function query(){
        	WarningService.warningScreenQuery(params()).success(function(res){
                $scope.tableData = res.result;
                $scope.totalCount = res.total;
            });
        }

		//分页查询
        $scope.$on('pageTool.skipPage',function(e,v){
            start = limit*(v-1);
            $scope.form.page = start;
            //$scope.onSubmit();
            query();
        });

        //页面点击查询
        $scope.onSubmit = function(){
        	start=0;
        	$scope.currentPage=1;
        	 query();
        };
        //初始化加载数据
        $scope.onSubmit();
        
         //重置
        $scope.resetBut = function(){
			$scope.form = {};
			$scope.form.warnDate = {};
		}
        
        //复选框选中
        $scope.checkRows = [];
        function findCheck(d) {
            for(var i=0;i< $scope.checkRows .length;i++) {
                var checkedData =  $scope.checkRows [i];
                if(checkedData.warnNo == d.warnNo) {
                    return i;
                }
            }
            return -1;
        }

        $scope.$on('fwatable.checked', function(e,d){
            if(findCheck(d) == -1) {
                $scope.checkRows.push(d);
            }
        });

        $scope.$on('fwatable.unchecked', function(e,d){
            var index = findCheck(d);
            if(index != -1) {
                $scope.checkRows.splice(index,1);
            }
        });
        
        //预警筛查-批量提交
		   $scope.submitBatchAnalysis = function(){	
			   if($scope.checkRows.length <= 0){
				   Modal.alert('请选择需要操作的选项！');
				   return;
			   }else{
				   var params = {
						  chooseRow : $scope.checkRows
				   }
				   Modal.confirm('确认批量操作吗？',function(){
					   $http.post(baseUrl + "/hcpmsWarning/batchAnalysisProcess.do",params).success(function(data){
						   if(data.resultMsg.length > 0 && data.resultMsg!=null && data.resultMsg!=""){
							   Modal.alert(data.resultMsg);
						   }else{
							   Modal.alert("批量操作成功！");
						   }
						   $scope.onSubmit();
					   });
				   });	
			   }
				
		   }
        
	}]);
})();